﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using iCollect.Chart;
using iCollect.Constants;
using System.Collections;
using iCollect.CustomComponent;
using iCollect.util;

namespace iCollect.CollectDetail
{
    public partial class PutongshujuChaxun : UserControl
    {
        private LineChartType chart1 = new LineChartType();
        private BaseDeviceCheck deviceCheck;

        private DateTime startTime, endTime;

        public PutongshujuChaxun()
        {
            InitializeComponent();

            chart1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Controls.Add(chart1);
        }

        private void Query_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“cUPDataDataSet.device”中。您可以根据需要移动或移除它。
            //this.normaldataTableAdapter.Fill(this.cUPDataDataSet2.normaldata);

        }

        public void loadDevice()
        {
            deviceCheck = UIUtil.getInstance().loadDeviceCheck(deviceCheck, deviceCheckPanel, HandleCustomEvent);
            //normaldataTableAdapter.GetDataBy();
            //normaldataTableAdapter.FillBy(this.cUPDataDataSet2.normaldata, GlobalConstants.currentDevice);
        }

        public void HandleCustomEvent(object sender, EventArgs e)
        {
            setChannelFilter();
        }

        private void dataChange(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                chart1.clear();
            }
            else
            {
                DataGridViewCellCollection cells = dataGridView1.SelectedRows[0].Cells;
                Boolean isManual = (Boolean)cells[4].Value;
                string channelID = cells[1].Value.ToString();
                DateTime cTime = (DateTime)cells[2].Value;
                getTimeRange();

                List<Double> dataList = new List<Double>();
                DataTable tb;
                if (isManual)
                {
                    if (dataGridView1.DataSource == normaldataBindingSource)
                    {
                        tb = normaldatamanualTableAdapter.GetDataBy(channelID, cTime,
                            GlobalConstants.currentDevice);
                    }
                    else
                    {
                        tb = exceptiondatamanualTableAdapter.GetDataBy(channelID, cTime,
                            GlobalConstants.currentDevice);
                    }

                    DataRow row = tb.Rows[0];
                    string dataString = row[2].ToString();
                    string2DoubleArray(dataString, dataList);
                    chart1.InitByChannel(channelID);
                    chart1.InitByData(dataList, channelID, DateTime.Parse(row[1].ToString()));
                }
                else
                {
                    List<DateTime> dateList = new List<DateTime>();
                    if (dataGridView1.DataSource == normaldataBindingSource)
                    {
                        tb = normaldataautoTableAdapter.GetDataBy(channelID, startTime,
                            GlobalConstants.currentDevice, endTime);
                    }
                    else
                    {
                        tb = exceptiondataautoTableAdapter.GetDataBy(channelID, startTime,
                            GlobalConstants.currentDevice, endTime);
                    }
                    foreach (DataRow row in tb.Rows)
                    {
                        dataList.Add(Double.Parse(row[2].ToString()));
                        dateList.Add(DateTime.Parse(row[1].ToString()));
                    }
                    chart1.InitByChannel(channelID);
                    chart1.InitByDateData(dataList, dateList, channelID);
                }
                
            }
        }

        private void string2DoubleArray(string str, List<Double> al)
        {
            string[] strArray = str.Split(',');
            for (int i = 0; i < strArray.Length; i++)
            {
                al.Add(Double.Parse(strArray[i]));
            }
        }

        private void btNormalQuery_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != normaldataBindingSource)
            {
                dataGridView1.DataSource = normaldataBindingSource;
            }
            
            if (rbCustom.Checked)
            {
                TimeSpan value = dateTimePickerEnd.Value - dateTimePickerStart.Value;
                if (value.Days > 30)
                {
                    alert("时间间隔不能大于30天。");
                    return;
                }
            }
            queryNormalData();
        }

        private void alert(string msg)
        {
            DialogResult MsgBoxResult;
            MsgBoxResult = MessageBox.Show(msg,//对话框的显示内容
            "提示",//对话框的标题
            MessageBoxButtons.OK,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Exclamation,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button1);//定义对话框的按钮式样
            if (MsgBoxResult == DialogResult.OK)
            {
                //this.Close();
            }
        }

        private void queryNormalData()
        {
            getTimeRange();
            normaldataTableAdapter.FillBy2(this.cupDataDataSet1.normaldata, GlobalConstants.currentDevice,
                startTime, endTime);
        }

        private void getTimeRange()
        {
            if (rbWeek.Checked)
            {
                startTime = DateTime.Parse(DateTime.Now.AddDays(-7).ToShortDateString());
                endTime = DateTime.Parse(DateTime.Now.AddDays(1).ToShortDateString());
            }
            else if (rbMonth.Checked)
            {
                startTime = DateTime.Parse(DateTime.Now.AddDays(-30).ToShortDateString());
                endTime = DateTime.Parse(DateTime.Now.AddDays(1).ToShortDateString());
            }
            else
            {
                startTime = DateTime.Parse(dateTimePickerStart.Value.ToShortDateString());
                endTime = DateTime.Parse(dateTimePickerEnd.Value.AddDays(1).ToShortDateString());
            }
        }

        //private void filterData()
        //{
        //    //this.normaldataBindingSource.Filter
        //    String filterStr;
        //    filterStr = "time > '" + dateTimePickerStart.Value.ToString() + "' and time <'" +
        //        dateTimePickerEnd.Value.ToString() + "'";
        //    int lastIndex;
        //    if (listBoxChannelType.SelectedItems.Count > 0)
        //    {
        //        filterStr += " and (";
        //        lastIndex = listBoxChannelType.SelectedItems.Count - 1;
        //        for (int i = 0; i < (lastIndex); i++)
        //        {
        //            filterStr += "type = '" + listBoxChannelType.SelectedItems[i].ToString() + "' or ";
        //        }
        //        filterStr += "type = '" + listBoxChannelType.SelectedItems[lastIndex] + "' ";
        //        filterStr += ")";
        //    }
        //    if (listBoxChannelID.SelectedItems.Count > 0)
        //    {
        //        filterStr += " and (";
        //        lastIndex = listBoxChannelID.SelectedItems.Count - 1;
        //        for (int i = 0; i < (lastIndex); i++)
        //        {
        //            filterStr += "code = '" + listBoxChannelID.SelectedItems[i].ToString() + "' or ";
        //        }
        //        filterStr += "code = '" + listBoxChannelID.SelectedItems[lastIndex] + "' ";
        //        filterStr += ")";
        //    }
        //    normaldataBindingSource.Filter = filterStr;
        //}

        private void resetFilter(object sender, EventArgs e)
        {
            normaldataBindingSource.Filter = null;
        }

        private void setChannelFilter()
        {
            String filterStr = "";
            List<String> channelIDList = deviceCheck.getCheckedChannels();
            int lastIndex;
            //if (listBoxChannelType.SelectedItems.Count > 0)
            //{
            //    filterStr += "(";
            //    lastIndex = listBoxChannelType.SelectedItems.Count - 1;
            //    for (int i = 0; i < (lastIndex); i++)
            //    {
            //        filterStr += "type = '" + listBoxChannelType.SelectedItems[i].ToString() + "' or ";
            //    }
            //    filterStr += "type = '" + listBoxChannelType.SelectedItems[lastIndex] + "' ";
            //    filterStr += ")";
            //}
            if (channelIDList.Count > 0)
            {
                filterStr += "(";
                lastIndex = channelIDList.Count - 1;
                for (int i = 0; i < (lastIndex); i++)
                {
                    filterStr += "code = '" + channelIDList.ElementAt(i) + "' or ";
                }
                filterStr += "code = '" + channelIDList.ElementAt(lastIndex) + "' ";
                filterStr += ")";
            }
            normaldataBindingSource.Filter = filterStr;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            deviceCheck.VTState(checkBox1.Checked);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            deviceCheck.PTState(checkBox2.Checked);
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            deviceCheck.TEState(checkBox3.Checked);
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            deviceCheck.FITState(checkBox4.Checked);
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void listBoxChannelID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timePanel.Visible = !timePanel.Visible;
        }

        private void btExceptionQuery_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != exceptiondataBindingSource)
            {
                dataGridView1.DataSource = exceptiondataBindingSource;
            }

            if (rbCustom.Checked)
            {
                TimeSpan value = dateTimePickerEnd.Value - dateTimePickerStart.Value;
                if (value.Days > 30)
                {
                    alert("时间间隔不能大于30天。");
                    return;
                }
            }
            queryExceptionData();
        }

        private void queryExceptionData()
        {
            getTimeRange();
            exceptiondataTableAdapter.FillBy2(this.cupDataDataSet1.exceptiondata, GlobalConstants.currentDevice,
                startTime, endTime);
        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void rbCustom_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbMonth_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbWeek_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void timePanel_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
